<html>
<head><meta charset="utf-8"><title>Omit `{}` in `impl X for X {}` · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60.html">Omit `{}` in `impl X for X {}`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="246334171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Omit%20%60%7B%7D%60%20in%20%60impl%20X%20for%20X%20%7B%7D%60/near/246334171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60.html#246334171">(Jul 17 2021 at 17:10)</a>:</h4>
<p>To accept only specific types for the parameter of a function you might do something like this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="kt">i8</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">a</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">A</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>

<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>The <code>impl X for X {}</code> pattern is definitely common. Has it ever been considered to allow writing <code>impl X for X;</code> because there's no body?</p>



<a name="246334258"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Omit%20%60%7B%7D%60%20in%20%60impl%20X%20for%20X%20%7B%7D%60/near/246334258" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60.html#246334258">(Jul 17 2021 at 17:13)</a>:</h4>
<p>Same for <code>trait A {}</code> I suppose. <code>struct A;</code> is allowed but not <code>trait A;</code>, why is that? Just because it's less common?</p>



<a name="246334824"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Omit%20%60%7B%7D%60%20in%20%60impl%20X%20for%20X%20%7B%7D%60/near/246334824" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60.html#246334824">(Jul 17 2021 at 17:29)</a>:</h4>
<p><code>struct A;</code> is not a shorthand for <code>struct A {}</code>, it behaves differently</p>



<a name="246334827"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Omit%20%60%7B%7D%60%20in%20%60impl%20X%20for%20X%20%7B%7D%60/near/246334827" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60.html#246334827">(Jul 17 2021 at 17:29)</a>:</h4>
<p>similarly, <code>fn f();</code> is not a shorthand for <code>fn f() {}</code></p>



<a name="246334873"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Omit%20%60%7B%7D%60%20in%20%60impl%20X%20for%20X%20%7B%7D%60/near/246334873" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60.html#246334873">(Jul 17 2021 at 17:30)</a>:</h4>
<p>so there's the possibility of causing some confusion when adding similar syntax that <em>does</em> mean the same</p>



<a name="246334883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Omit%20%60%7B%7D%60%20in%20%60impl%20X%20for%20X%20%7B%7D%60/near/246334883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60.html#246334883">(Jul 17 2021 at 17:30)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink  [he/him]</span> <a href="#narrow/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60/near/246334827">said</a>:</p>
<blockquote>
<p>similarly, <code>fn f();</code> is not a shorthand for <code>fn f() {}</code></p>
</blockquote>
<p>But that doesn't even exist does it?</p>



<a name="246334890"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Omit%20%60%7B%7D%60%20in%20%60impl%20X%20for%20X%20%7B%7D%60/near/246334890" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60.html#246334890">(Jul 17 2021 at 17:31)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink  [he/him]</span> <a href="#narrow/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60/near/246334873">said</a>:</p>
<blockquote>
<p>so there's the possibility of causing some confusion when adding similar syntax that <em>does</em> mean the same</p>
</blockquote>
<p>That is true</p>



<a name="246334893"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Omit%20%60%7B%7D%60%20in%20%60impl%20X%20for%20X%20%7B%7D%60/near/246334893" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60.html#246334893">(Jul 17 2021 at 17:31)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink  [he/him]</span> <a href="#narrow/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60/near/246334824">said</a>:</p>
<blockquote>
<p><code>struct A;</code> is not a shorthand for <code>struct A {}</code>, it behaves differently</p>
</blockquote>
<p>What is the difference?</p>



<a name="246334966"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Omit%20%60%7B%7D%60%20in%20%60impl%20X%20for%20X%20%7B%7D%60/near/246334966" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60.html#246334966">(Jul 17 2021 at 17:33)</a>:</h4>
<p><span class="user-mention silent" data-user-id="376093">ruster</span> <a href="#narrow/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60/near/246334883">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink  [he/him]</span> <a href="#narrow/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60/near/246334827">said</a>:</p>
<blockquote>
<p>similarly, <code>fn f();</code> is not a shorthand for <code>fn f() {}</code></p>
</blockquote>
<p>But that doesn't even exist does it?</p>
</blockquote>
<p>It exists in traits, <code>fn f() {}</code> defines a default implementation of <code>f</code> while <code>fn f();</code> requires all impls to provide an implementation of <code>f</code>.</p>



<a name="246334969"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Omit%20%60%7B%7D%60%20in%20%60impl%20X%20for%20X%20%7B%7D%60/near/246334969" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60.html#246334969">(Jul 17 2021 at 17:33)</a>:</h4>
<p><span class="user-mention silent" data-user-id="376093">ruster</span> <a href="#narrow/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60/near/246334893">said</a>:</p>
<blockquote>
<p>What is the difference?</p>
</blockquote>
<p><code>struct A;</code> is like: <code>struct A {} const A: A = A {};</code></p>



<a name="246335265"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Omit%20%60%7B%7D%60%20in%20%60impl%20X%20for%20X%20%7B%7D%60/near/246335265" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60.html#246335265">(Jul 17 2021 at 17:41)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink  [he/him]</span> <a href="#narrow/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60/near/246334966">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="376093">ruster</span> <a href="#narrow/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60/near/246334883">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink  [he/him]</span> <a href="#narrow/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60/near/246334827">said</a>:</p>
<blockquote>
<p>similarly, <code>fn f();</code> is not a shorthand for <code>fn f() {}</code></p>
</blockquote>
<p>But that doesn't even exist does it?</p>
</blockquote>
<p>It exists in traits, <code>fn f() {}</code> defines a default implementation of <code>f</code> while <code>fn f();</code> requires all impls to provide an implementation of <code>f</code>.</p>
</blockquote>
<p>Oh, right, I thought you were talking about the top level</p>



<a name="246335338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Omit%20%60%7B%7D%60%20in%20%60impl%20X%20for%20X%20%7B%7D%60/near/246335338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60.html#246335338">(Jul 17 2021 at 17:43)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232018">Daniel Henry-Mantilla</span> <a href="#narrow/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60/near/246334969">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="376093">ruster</span> <a href="#narrow/stream/122651-general/topic/Omit.20.60.7B.7D.60.20in.20.60impl.20X.20for.20X.20.7B.7D.60/near/246334893">said</a>:</p>
<blockquote>
<p>What is the difference?</p>
</blockquote>
<p><code>struct A;</code> is like: <code>struct A {} const A: A = A {};</code></p>
</blockquote>
<p>Interesting.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>